//
//  AppDelegate.m
//  Zyachting
//
//  Created by hungnt47 on 11/10/2012.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"
#import "SearchViewController.h"
#import "MapViewController.h"
#import "ContactViewController.h"
#import "AboutViewController.h"
static NSString* kAppId = @"425726260821083";

@implementation AppDelegate

@synthesize window = _window;
@synthesize tabBarController = _tabBarController;
@synthesize currentLocation = _currentLocation;
@synthesize locationManager = _locationManager;
@synthesize facebook;
@synthesize userPermissions;

@synthesize currentMessage,currentPostImage;
- (void)dealloc
{
    [_window release];
    [_tabBarController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self];
    
    // Check and retrieve authorization information
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }
    self.userPermissions = [[NSArray alloc] initWithObjects:@"offline_access",@"publish_stream", nil];
    //--Tw 

    
    self.locationManager = [[[CLLocationManager alloc] init] autorelease];
    [self.locationManager setDesiredAccuracy:CLLocationDistanceMax];
    [self.locationManager setDelegate:self];
    [self.locationManager startUpdatingLocation];
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    UIViewController *viewController1 = [[[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil] autorelease];
    UINavigationController* nav1 = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];
    [nav1.navigationBar setBarStyle:UIBarStyleBlackOpaque];
    UIViewController *viewController2 = [[[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil] autorelease];
//    [nav1.navigationBar setTintColor:[UIColor colorWithRed:192.0f/256.0f green:192.0f/256.0f blue:192.0f/256.0f alpha:1.0f]];
    UINavigationController* nav2 = [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease];    
    [nav2.navigationBar setBarStyle:UIBarStyleBlackOpaque];    
    UIViewController *viewController3 = [[[ContactViewController alloc] initWithNibName:@"ContactViewController" bundle:nil] autorelease];    
    UINavigationController* nav3 = [[[UINavigationController alloc] initWithRootViewController:viewController3] autorelease];      
    [nav3.navigationBar setBarStyle:UIBarStyleBlackOpaque];
    
    UIViewController* viewController4 = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:nil];
    UINavigationController* nav4 = [[UINavigationController alloc] initWithRootViewController:[viewController4 autorelease]];
    [nav4.navigationBar setBarStyle:UIBarStyleBlackOpaque];
    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1, nav2, nav3,nav4,nil];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
     */
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    /*
     Called when the application is about to terminate.
     Save data if appropriate.
     See also applicationDidEnterBackground:.
     */
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [self.facebook handleOpenURL:url];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [self.facebook handleOpenURL:url];
}
/*
// Optional UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
}
*/

/*
// Optional UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
}
*/

#pragma mark -CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
	didUpdateToLocation:(CLLocation *)newLocation
		   fromLocation:(CLLocation *)oldLocation
{
    self.currentLocation = newLocation;

#if TARGET_IPHONE_SIMULATOR
    self.currentLocation =  [[[CLLocation alloc] initWithLatitude:38.08201394999351 longitude:-0.9391593933105469] autorelease];
#endif

}

#pragma mark - FBSession Delegate
- (void)fbDidLogin
{
    NSNotification* notification = [NSNotification notificationWithName:@"FacebookDidLoginNotification" object:nil];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
}

-(void)tweetWithMedia:(UIImage*)media andMessage:(NSString*)message
{
//    UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _engine delegate: self];
//    
//    if (controller){
//    [self.tabBarController presentModalViewController: controller animated: YES];
//    self.currentMessage = message;
//    self.currentPostImage = media;
//    }
//    else {
//        if (self.currentPostImage) {
//            [_engine sendUpdate:self.currentMessage withMedia:self.currentPostImage lat:nil lon:nil];
//        }else{
//            [_engine sendUpdate:self.currentMessage];
//        }
//    }
}

@end
